package DataStruct;

import android.util.Log;

import org.w3c.dom.Node;

@SuppressWarnings("unchecked")
public class SingleLinkedList <E> extends AbstractList<E> {
    /**
     * 头节点
     */
    private Node<E> first;
    
    /**
     * 链表节点
     * lianglibao
     * 2019/5/23
     * 3:41 PM
     */
    private static class Node <E> {
        E element;
        Node<E> next;
        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }

    /**
     * 获取index位置节点
     * lianglibao
     * 2019/5/23 
     * 4:03 PM
     */
    private Node <E> node(int index) {
        rangeCheck(index);
        
        Node node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }


    @Override
    public void clear() {
        first = null;
        size = 0;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        if (index == 0) {
            Log.d("SingleLinkedList", "prev:null," + "now:" + element);
            first = new Node<>(element, first);
        } else {
            Node prev = node(index - 1);
            Log.d("SingleLinkedList", "prev:" + prev.element + "," + "now:" + element);
            prev.next = new Node(element, prev.next);
        }
        size ++;
    }


    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            Node node = first;
            for (int i = 0; i < size ; i++) {
             if (element.equals(node.element)) return i;
             node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public E set(int index, E elemant) {
        rangeCheck(index);
        Node <E>node = node(index);
        E oldElement = node.element;
        node.element = elemant;
        return oldElement;
    }

    @Override
    public E get(int index) {
        rangeCheck(index);
        return (E) node(index).element;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);

        Node <E> node = first;
        if (index == 0) {
            first = first.next;
        } else {
            Node prev = node(index - 1);
            node = prev.next;
            prev.next = node.next;
        }
        size --;
        return node.element;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        Node node = first;
        if (node != null) {
            sb.append("first" + "_" + node.element);
        } else {
            sb.append("first->null");
        }

        for (int i = 0; i < size; i++) {
            node = node.next;
            if (node != null) {
                sb.append("->" + node.element);
            } else {
                sb.append("->null" );
            }
        }
        return sb.toString();
    }

    /**
     * 打印反转后的链表
     * lianglibao
     * 2019/5/24
     * 5:54 PM
     */
    public String printReverList() {
        Node node = reversalList2();
        StringBuffer sb = new StringBuffer();
        if (node != null) {
            sb.append("first" + "_" + node.element);
        } else {
            sb.append("first->null");
        }

        for (int i = 0; i < size; i++) {
            node = node.next;
            if (node != null) {
                sb.append("->" + node.element);
            } else {
                sb.append("->null" );
            }
        }
        return sb.toString();
    }

    private Node reversalList1 (Node head) {
        if (head == null || head.next == null) {
            return head;
        }
        Node newHeader = reversalList1(head.next);
        head.next.next = head;
        head.next = null;
        return  newHeader;
    }

    // 非递归实现反转!
    private Node reversalList2() {
        Node newHeader = null;
        while (first != null) {
            Node tmp = first.next;
            first.next = newHeader;
            newHeader = first;
            first = tmp;
        }
        return newHeader;
    }

}
